﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:Sms_filter三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Sms_filterService
	{
		#region 增加一条数据
		public static int AddSms_filter(Sms_filter sms_filter)
		{
			string sql =
			"INSERT INTO Sms_filter(words) "+
			"VALUES (@words)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@words",sms_filter.Words)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSms_filterById(long id)	
		{
			string sql="DELETE "+"Sms_filter"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySms_filterByid(Sms_filter sms_filter)
		{
		string sql ="UPDATE "+"Sms_filter"+" set "+
					"words=@words "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",sms_filter.Id),
					new SqlParameter("@words",sms_filter.Words)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Sms_filter> GetAllSms_filters()
		{
			string sqlAll = "SELECT * FROM Sms_filter";
			return GetSms_filtersBySql(sqlAll);
		}
		//以标识列获得对象
		public static Sms_filter GetSms_filterByid(object id)
		{
			string sql = "SELECT * FROM "+"Sms_filter"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Sms_filter sms_filter = new Sms_filter();
					sms_filter.Id=Convert.ToInt32(reader["id"]);
					sms_filter.Words=Convert.ToString(reader["words"]);
					reader.Close();
					
					return sms_filter;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Sms_filter> GetSms_filtersBySql(string safeSql)
		{
			List<Sms_filter> list = new List<Sms_filter>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_filter sms_filter = new Sms_filter();
					sms_filter.Id=Convert.ToInt32(row["id"]);
					sms_filter.Words=Convert.ToString(row["words"]);
					list.Add(sms_filter);
				}
				return list;
			}
             
		}
		private static IList<Sms_filter> GetSms_filtersBySql( string sql, params SqlParameter[] values )
		{
			List<Sms_filter> list = new List<Sms_filter>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_filter sms_filter = new Sms_filter();
					sms_filter.Id=Convert.ToInt32(row["id"]);
					sms_filter.Words=Convert.ToString(row["words"]);
					list.Add(sms_filter);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Sms_filter> ShowSms_filterForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from Sms_filter where  id not in ( select top "
						   +number+" id from Sms_filter order by id desc ) order by id desc";
			return GetSms_filtersBySql(strsql);
		}
		#endregion
	}
}

